A total of 1950 characters, expected to take 5 minutes to complete reading.
Install Qt
Download
In order to use CLion to develop Qt program, you have to have Qt Compiler it. Then let's download Qt first.
open the chinese official website of the qt:https://www.qt.io/zh-cn/
Click in the upper right corner Download and Try:
Click Community Users-Explore Qt Community Edition:
Go down and find:
Then download the version of your system here:
The above record is to prevent the website from being unable to find the download location after the revision. It is faster to download directly to the download address in the last step:https://www.qt.io/download-qt-installer-oss
install
After downloading, open the installation package to install:
Here you can refer to other blogger tutorials and customize the installation to install the components you want.
Installing CLion
CLion Yes C Language of IDE Compiler, very easy to use.
Download from here The latest CLion, then fool-like installation:
New Qt Project
We open up CLion and find the top left corner File-New-Project:
Find the left Qt Widget Executables:
Here. Qt CMake Prefix Path Select your Qt installation directory Qt directory \Qt version number \mingw_64For example, here I am. QT\6.7.0\mingw_64 You can. Then we can create the project by clicking Create in the lower right corner!
After creating a new project, we click under the project folder main.cpp:
I am here to prompt that the project is not configured, click Fixed-Select CMakeLists.txt You can:
If there are no other problems, we click Run in the upper right corner, which will compile successfully and display a basic window:
This shows that my project is ready to run!
Refactoring Project
We can find that the default new project file structure is relatively simple, all in the root directory, with the traditional C program layout is not the same.
Create the following directory under the root directory:
Directory Name | Role |
---|---|
src | Original file, such as. cpp |
include | A header file, such as. h |
form | Interface files, such as. ui .h .cpp |
lib | Store third-party libraries |
resource | Store resource files |
build | Compile the output file |
By the way, put it again. main.cpp Put it in the src directory.
Then we open CMakeLists.txt, add the following statement:
# 搜索 src 目录
aux_source_directory(src SRC)
# 搜索 form 目录
aux_source_directory(form FORM)
# 包含 include 目录下的所有文件
include_directories(include)
# 包含 form 目录下的头文件
include_directories(form)
# 编译所有 src 和 form 目录下的文件
# 注意:这语句本来就有 只需修改为“add_executable(你的项目名称 ${SRC} ${FORM})”即可
add_executable(3DSystemHelper ${SRC} ${FORM})
# 设置编译输出的目录为 build 目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
At this time, rebuild the project and you can find the runable files that have been generated in the build directory:
This is also can run oh!
Then let's try creating a new interface under the form directory:
Then modify the main.cpp file:
#include "mainwindow.h"
#include "QApplication"
int main(int argc, char *argv[]) {QApplication a(argc, argv);
// 创建 mainwindow 对象
mainwindow mw;
// 显示窗口
mw.show();
return QApplication::exec();}
Run through:
Problems encountered
Cannot find libxxx.dll file
Method 1: Manually add
We run the compiled in the build directory. exe You may encounter this problem when you file.
This is because there is no use of Qt's Dynamic Link Library. We find QT directory \QT version number \mingw_64\bin directory, for example in my this is QT\6.7.0\mingw_64\bin, enter and create here. cmd window, enter:
# 加载环境
qtenv2.bat
# 动态链接库
windeployqt 你的文件.exe
Then many other files are generated in the build directory, and then the run becomes:
Method 2: Modify CMakeLists.txt
In CMakeLists.txt Add the following code, will 3DSystemHelper
Modify the name of the executable generated for you:
add_custom_command(TARGET 3DSystemHelper POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Start deploy Qt..."
COMMAND ${CMAKE_PREFIX_PATH}/bin/qtenv2.bat
COMMAND windeployqt ${EXECUTABLE_OUTPUT_PATH}/3DSystemHelper.exe
COMMAND ${CMAKE_COMMAND} -E echo "Deploy Qt completed!"
)
Then rebuild it:
The result is still the same, can run: